package com.shuzijun.leetcode.editor.cn;

import java.util.LinkedList;
import java.util.Stack;

public class ZhanDeYaRuDanChuXuLieLcof{
    public static void main(String[] args) {
         Solution solution = new ZhanDeYaRuDanChuXuLieLcof().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int j = 0;
        int k = 0;
        while (k < pushed.length + popped.length){
            k++;
            if (!stack.isEmpty() && j < popped.length && stack.peek() == popped[j]){
                stack.pop();
                j++;
                continue;
            }

            if (i < pushed.length){
                stack.push(pushed[i]);
                i++;
                continue;
            }
            return false;
        }
        return true;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}